package rn.automacao.persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import rn.automacao.banco.Conexao;
import rn.automacao.dominio.AtendimentoVO;

public class AtendimentoDAO implements ObjetoDAO {

	private Connection conn = null;

	private PreparedStatement ps = null;

	private ResultSet rs = null;

	public void alterar(Object objeto) {

		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn
					.prepareStatement("UPDATE ATENDIMENTO SET DATAABERTURA=?, DATAEXECUCAO=?, VALOR=?, FUNCIONARIO=? WHERE NUMEROOS=?");
			ps.setDate(1, ((AtendimentoVO) objeto).getDataAbertura());
			ps.setDate(2, ((AtendimentoVO) objeto).getDataExecucao());
			ps.setDouble(3, ((AtendimentoVO) objeto).getValor());
			ps.setInt(4, ((AtendimentoVO) objeto).getFuncionario().getCodigo());
			//ps.setInt(5, ((AtendimentoVO) objeto).getNumeroOS());
			ps.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				ps.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void cadastrar(Object objeto) {

		conn = Conexao.getInstance().abreConexao();
		AtendimentoVO atend = (AtendimentoVO)objeto;
		try {
			ps = conn.prepareStatement("INSERT INTO OS (CODOS, ITEM, CODFUNC, TIPO_OS, DATA_ABERTURA, DATA_EXECUCAO, VALOR, OBSERVACAO, PAGA) VALUES (?,?,?,?,?,?,?,?,?)");
			ps.setInt(1, atend.getCodOS());
			ps.setInt(2, atend.getItemOS());
			ps.setInt(3, atend.getFuncionario().getCodigo());
			ps.setInt(4, atend.getTipoOS().getCodtipo());
			ps.setDate(5, atend.getDataAbertura());
			ps.setDate(6, atend.getDataExecucao());
			ps.setDouble(7, atend.getValor());
			ps.setString(8, atend.getObs());
			ps.setInt(9, atend.getPaga());
			ps.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				ps.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void deletar(Object objeto) {

		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn
					.prepareStatement("DELETE FROM ATENDIMENTO WHERE NUMEROOS=?");
//			ps.setInt(1, ((AtendimentoVO) objeto).getNumeroOS());
			ps.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				ps.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public Collection<Object> listar() {

		Collection<Object> atendimentos = new ArrayList<Object>();
		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("SELECT * FROM ATENDIMENTO");
			rs = ps.executeQuery();

			while(rs.next()) {
				//atendimentos.add(new AtendimentoVO(rs.getInt(1), rs.getDate(2), rs.getDate(3),
						//rs.getDouble(4), ((FuncionarioVO)new FuncionarioDAO().pesquisar(rs.getInt(5)))));
			}
			return atendimentos;

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Collection<Object> pesquisar(String param, String filtro) {

		Collection<Object> atendimentos = new ArrayList<Object>();
		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("SELECT * FROM ATENDIMENTO WHERE FUNCIONARIO LIKE '%?%'");
			ps.setString(1, param);
			rs = ps.executeQuery();

			while(rs.next()) {
				//atendimentos.add(new AtendimentoVO(rs.getInt(1), rs.getDate(2), rs.getDate(3),
						//rs.getDouble(4), ((FuncionarioVO)new FuncionarioDAO().pesquisar(rs.getInt(5)))));
			}
			return atendimentos;

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return null;
	}

	public Object pesquisar(int param, String filtro) {

		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("SELECT * FROM ATENDIMENTO");
			rs = ps.executeQuery();

			rs.next();
			//return new AtendimentoVO(rs.getInt(1), rs.getDate(2), rs.getDate(3),
						//rs.getDouble(4), ((FuncionarioVO)new FuncionarioDAO().pesquisar(rs.getInt(5))));

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Object pesquisarLogin(String login, String senha) {
		// TODO Auto-generated method stub
		return null;
	}

}
